package com.example.domain.dto.pricelist;

import com.example.domain.entity.PriceList;
import com.example.domain.entity.enums.PriceListStatus;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

/**
 * 价目表摘要DTO（不包含价格条目，用于列表展示）
 * 
 * @author Means
 * @since 2025-10-27
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class PriceListSummaryDTO {
    
    /**
     * 价目表ID
     */
    private String id;
    
    /**
     * 价目表名称
     */
    private String name;
    
    /**
     * 生效开始时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime effectiveFrom;
    
    /**
     * 生效结束时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime effectiveTo;
    
    /**
     * 状态
     */
    private PriceListStatus status;
    
    /**
     * 状态描述
     */
    private String statusDesc;
    
    /**
     * 条目数量
     */
    private Integer entryCount;
    
    /**
     * 是否当前生效
     */
    private Boolean isCurrentlyEffective;
    
    /**
     * 创建时间
     */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createdAt;
    
    /**
     * 从实体类创建DTO
     * 
     * @param priceList 价目表实体
     * @return PriceListSummaryDTO
     */
    public static PriceListSummaryDTO from(PriceList priceList) {
        if (priceList == null) {
            return null;
        }
        
        LocalDateTime now = LocalDateTime.now();
        boolean isCurrentlyEffective = priceList.getStatus() == PriceListStatus.ACTIVE
                && !priceList.getEffectiveFrom().isAfter(now)
                && (priceList.getEffectiveTo() == null || !priceList.getEffectiveTo().isBefore(now));
        
        return PriceListSummaryDTO.builder()
                .id(priceList.getId())
                .name(priceList.getName())
                .effectiveFrom(priceList.getEffectiveFrom())
                .effectiveTo(priceList.getEffectiveTo())
                .status(priceList.getStatus())
                .statusDesc(getStatusDescription(priceList.getStatus()))
                .isCurrentlyEffective(isCurrentlyEffective)
                .createdAt(priceList.getCreatedAt())
                .build();
    }
    
    /**
     * 获取状态描述
     * 
     * @param status 状态
     * @return 状态描述
     */
    private static String getStatusDescription(PriceListStatus status) {
        if (status == null) {
            return null;
        }
        switch (status) {
            case ACTIVE:
                return "激活";
            case INACTIVE:
                return "未激活";
            default:
                return status.name();
        }
    }
}

